<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 头部布局 -->
        <template>
          <div class="department-container">
            <div class="app-container">
              <el-card>
                <!-- 用一个行列布局 -->
                <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
                  <el-col :span="20">
                    <svg-icon icon-class="tree-table" /><span>{{ company.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <!-- 两个内容 -->
                      <el-col>负责人</el-col>
                      <!-- 下拉菜单 element -->
                      <el-col>
                        <el-dropdown>
                          <span>
                            操作<i class="el-icon-arrow-down" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </el-card>
            </div>
          </div>
        </template>
        <!--树状布局  -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template slot-scope="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon :icon-class="geticonname(data)" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="deleteFn(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹出层 -->
        <el-dialog :title="isEdit?'编辑':'添加'" :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="showDialog" @close="hclose">
          <AddorEdit :id="curId" ref="deptDialog" :list="originList" :edit="isEdit" @success="hSuccess" @hiden="Hhiden" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import AddorEdit from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: { AddorEdit },
  data() {
    return {
      list: [], //
      showDialog: false,
      isEdit: false,
      company: {},
      curId: '',
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 更新用户信息
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 保存数据
        // shift会取出数组第一个元素，同时会改变源数组
        const company = res.data.depts.shift()
        this.company = company
        this.originList = res.data.depts.map(item => {
          return {
            code: item.code,
            id: item.id,
            pid: item.pid,
            name: item.name
          }
        })
        console.log(this.originList)
        // company.children = tranListToTreeData(res.data.depts)
        this.list = tranListToTreeData(res.data.depts)
        // 后端返回的数据中 第一条是公司信息，它应该直接就是tree的根节点
        // 为了后面计算方便，把它直接取出来
      } catch (err) {
        console.log(err)
      }
    },
    deleteFn(data) {
      if (data.children.length === 0) {
        this.hDel(data.id)
      }
    },
    // 用户点击删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.deptDialog.loadDetail()
      })
    },
    hDel(id) {
      // 删除时第一步先弹窗询问
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 做删除功能
          this.doDel(id)
        })
        .catch(() => {})
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    Hhiden() {
      this.showDialog = false
    },
    hclose() {
      this.$refs.deptDialog.resetForm()
    },
    geticonname(data) {
      // data.children.length === 0 ? 'people' : 'tree'
      if (data.children.length === 0) {
        return 'people'
      } else if (data.pid === '') {
        return 'link'
      } else {
        return 'tree'
      }
    }
  }
}
</script>

<style>
</style>
